package com.hongxun.design.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.Looper;

import com.hongxun.design.MyApp;
import com.hongxun.design.projectutil.ProjectCache;

/**
 * Created by zerdoor_pc on 2016/7/19.
 */
public class CacheService extends Service {
    Thread thread;
    /**
     * 在 Local Service 中直接继承 Binder 而不是 IBinder,因为 Binder 实现了 IBinder 接口，这样我们可以少做很多工作。
     *
     * @author newcj
     */
    public class SimpleBinder extends Binder {
        /**
         * 获取 Service 实例
         *
         * @return
         */
        public CacheService getService() {
            return CacheService.this;
        }

        public Thread getthread(){
            return  thread;
        }

    }

    public SimpleBinder sBinder;

    @Override
    public void onCreate() {
        super.onCreate();
        // 创建 SimpleBinder
        sBinder = new SimpleBinder();
        startCache();
    }

    void startCache() {

        if (MyApp.isOnLine){
            thread=  new Thread(new Runnable() {
                @Override
                public void run() {
                    Looper.prepare();
                    new ProjectCache(sBinder.getService());
                    Looper.loop();
                }
            });
            thread.start();
        }

    }

    @Override
    public IBinder onBind(Intent intent) {
        // 返回 SimpleBinder 对象
        return sBinder;
    }


}

